#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Zobrist hash算法实现
author: yooongchun@foxmail.com
"""
import secrets

from util import Chess


class Zobrist(object):
    """zobrist hash 算法计算盘面"""
    
    def __init__(self, size: int):
        self._size = size
        self.white = [secrets.randbits(64) for _ in range(size**2)]
        self.black = [secrets.randbits(64) for _ in range(size**2)]
        self._current = secrets.randbits(64)

    def go(self, row: int, col: int, chess:int):
        """获取对应位置的新hash值"""
        idx = row * self._size + col
        self._current ^= (self.black[idx] if chess == Chess.BLACK else self.white[idx])
        return self._current

    def reverse(self):
        """反转"""
        self.white, self.black = self.black, self.white
        return True
